package src.E_Thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

// 线程池
public class E18_ThreadPool {
    public static void main(String[] args) {
        // 1、提供指定线程数量的线程池
        ExecutorService service = Executors.newFixedThreadPool(10);
        ThreadPoolExecutor tpe = (ThreadPoolExecutor) service;
        tpe.setCorePoolSize(50); // 设置线程池中线程数的上限
        // 2、执行线程
        tpe.execute(new ThreadA()); // 适用于继承Thread类或实现Runnable接口的线程
        tpe.execute(new ThreadB()); // 适用于继承Thread类或实现Runnable接口的线程
        tpe.submit(new ThreadC()); // 适用于实现Callable接口的线程
        // 3、关闭连接池
        tpe.shutdown();
    }
}

class ThreadA extends Thread{
    @Override
    public void run() {
        Thread.currentThread().setName("老幺");
        System.out.println(Thread.currentThread().getName());
    }
}

class ThreadB implements Runnable{
    @Override
    public void run() {
        Thread.currentThread().setName("老贰");
        System.out.println(Thread.currentThread().getName());
    }
}

class ThreadC implements Callable<Object> {
    @Override
    public Object call() throws Exception {
        Thread.currentThread().setName("老叁");
        System.out.println(Thread.currentThread().getName());
        return null;
    }
}